//js和jsx的交互接口
var cs = new CSInterface();

/*
 * 类型: PS -> Unity类型映射
 * 文本:TextItem -> UGUI(Text)
 * 图层:ArtLayer -> UGUI(Image)
 * 组别:LayerSet -> UGUI(Rect Transform)
 */
var Component = {
    TextItem : "TextItem",
    ArtLayer : "ArtLayer",
    LayerSet : "LayerSet",
}

function loadJSX(fileName)
{
    try {
        var extensionRoot = cs.getSystemPath(SystemPath.EXTENSION) + "/jsx/";
        //var path = extensionRoot + fileName;
        //alert(path);
        cs.evalScript('$.evalFile("' + extensionRoot + fileName + '")');
    } catch (error) {
        console.log(error);
    }
}

loadJSX("json2.js"); //为 JAX 载入 JSON 库

function createPsdNode(map) {
    var json = JSON.stringify(map);
    console.log(json);
    //alert(map);
    cs.evalScript("createPsdNode('" + json + "')", function(result) {
        if (result != "ok!") {
            alert(result);
        }
    });
}

function onClickCreateNode(map) {
    createPsdNode(map);
}

function bindEventListener(elementId, callback) {
    var e = document.getElementById(elementId)
    if (e == null) {
        alert("elementId [" + elementId + "] is null")
        return
    }
    if (callback) {
        e.addEventListener("click", callback)
    }
}

bindEventListener("btn_9s_create", function() {
    var attr = "Image_9S:";
    var value = document.getElementById("input_scale9").value;
    var value = attr + value;
    var map = {};
    map[value] = Component.ArtLayer+':300,300,200,200,c8c8c8';
    createPsdNode(map);
})

bindEventListener("btn_9s_1_create",function(){
    var attr = "Image_9S:";
    var value = "";
    for (var i = 1; i <= 4; i++)
    {
        var name = "input_scale9_" + i;
        var v = document.getElementById(name).value;
        value += v;
        if (i != 4) value += ":";
    }
    var value = attr + value;
    var map = {};
    map[value] = Component.ArtLayer+':300,300,200,200,c8c8c8';
    createPsdNode(map);
})

bindEventListener("btn_attr_root_create", function() {
    var attr = "@Root";
    var value = attr;
    var map = {};
    map[value] = Component.LayerSet;
    createPsdNode(map);
})

bindEventListener("btn_attr_texture_create", function() {
    var attr = "Image_Texture";
    var value = attr;
    var map = {};
    map[value] = Component.ArtLayer+':300,300,200,200,c8c8c8';
    createPsdNode(map);
})

bindEventListener("btn_slider_create", function() {
    var attrs = [ "L", "R", "T", "B" ];
    var index = document.getElementById("select_slider").selectedIndex;
    var attr = "Slider@Slider:" + attrs[index];
    var value = attr;
    var map = {};
    map[value] = {
        //'@Size' : Component.ArtLayer+':300,300,400,50,c8c8c8',
        'Slider_Bg@Common' : Component.ArtLayer+':120,300,406,36,c8c8c8',
        'Slider_Fill@Common' : Component.ArtLayer+':123,303,400,30,7ecef4',
        'Slider_Handle@Common' : Component.ArtLayer+':180,294,50,50,7ecef4',
    };
    createPsdNode(map);
})

bindEventListener("btn_input_create", function() {
    var attr = "InputField@InputField";
    var value = attr;
    var map = {};
    map[value] = {
        'InputField_Background@Common' : Component.ArtLayer+':149,272,450,75,c8c8c8',
        'InputField_Holder' : Component.TextItem+':173,320,30,ffffff,请输入默认文本',
    };
    createPsdNode(map);
})

bindEventListener("btn_button_create", function() {
    var attr = "Button@Button";
    var value = attr;
    var map = {};
    map[value] = {
        //'Button_Disabled@Common' : Component.ArtLayer,
        //'Button_HighLighted@Common' : Component.ArtLayer,
        'Button_Pressed@Common' : Component.ArtLayer+':300,300,200,80,b5b5b5',
        'Button_Normal@Common' : Component.ArtLayer+':300,300,200,80,c8c8c8',
        'Button_Text' : Component.TextItem+':340,350,30,ffffff,默认文本',
    };
    createPsdNode(map);
})

bindEventListener("btn_scrollview_create", function() {
    var attrs = [ "H", "V" ];
    var index = document.getElementById("select_scrollview").selectedIndex;
    var attr = "ScrollView@ScrollView:" + attrs[index];
    var value = attr;
    var map = {};
    map[value] = {
        '@Size' : Component.ArtLayer+':203,212,356,165,c8c8c8',
        'Item_2@LE' : {
            'ItemBackground@Common' : Component.ArtLayer+':218,302,327,61,7ecef4'
        },
        'Item_1@LE' : {
            'ItemBackground@Common' : Component.ArtLayer+':218,226,327,61,7ecef4'
        },
    };
    createPsdNode(map);
})

bindEventListener("btn_toggle_create", function() {
    var attr = "Toggle@Toggle";
    var value = attr;
    var map = {};
    map[value] = {
        'Toggle_Background@Common' : Component.ArtLayer+':307,274,62,62,c8c8c8',
        'Toggle_Checkmark@Common' : Component.ArtLayer+':316,282,45,45,7ecef4',
        'Toggle_Text' : Component.TextItem+':180,314,30,ffffff,默认文本',
    };
    createPsdNode(map);
})

bindEventListener("btn_tabgroup_create", function() {
    var name = document.getElementById("input_tabgroup_tab").value;
    var attr = "TabGroup@TabGroup";
    var value = attr;
    var group_name = "TabGroup_Tab:" + name;
    var map = {};
    var map_group = {};
    map_group[group_name] = {
        'TabGroup_TabUnSel@Common' : Component.ArtLayer+':307,274,62,62,c8c8c8',
        'TabGroup_TabSel@Common' : Component.ArtLayer+':316,282,45,45,7ecef4',
    }
    map[value] = map_group;
    createPsdNode(map);
})